﻿using System;

class Tasks
{
    //Exercise 13.
    //Write a program that can solve these tasks:
    // - Reverses the digits of a number
    // - Calculates the average of a sequence of integers
    // - Solves a linear equation a * x + b = 0
    //Create appropriate methods.
    //Provide a simple text-based menu for the user to choose which task to solve.
    //Validate the input data:
    // - The decimal number should be non-negative
    // - The sequence should not be empty
    // - a should not be equal to 0

    static void Main(string[] args)
    {
        Console.WriteLine("Choose which task to solve (type the number of the choise):");
        Console.WriteLine("1. Reverse the digits of a number");
        Console.WriteLine("2. Calculate the average of a sequence of integers");
        Console.WriteLine("3. Solve a linear equation a * x + b = 0");
        int choice = int.Parse(Console.ReadLine());

        if (choice == 1)
        {
            Console.Write("Type a decimal number: ");
            string number = Console.ReadLine();
            int numberInt = int.Parse(number);
            if (numberInt < 0)
            {
                Console.WriteLine("The number should be positive!");
            }
            ReverseDigits(number, numberInt);
        }
        else if (choice == 2)
        {
            //initialize the array
            Console.Write("Length of the sequence = ");
            int size = int.Parse(Console.ReadLine());
            int[] array = new int[size];
            Random random = new Random();
            for (int i = 0; i < size; i++)
            {
                array[i] = random.Next(0, 100);
            }
            Console.WriteLine("The average of a sequence is: {0:0.00}", Average(size, array));
        }
        else if (choice == 3)
        {
            Console.Write("Enter a: ");
            double a = double.Parse(Console.ReadLine());
            Console.Write("Enter b: ");
            double b = double.Parse(Console.ReadLine());
            LinearEquation(a, b);
        }
        else
        {
            Console.WriteLine("Invalid entry!");
        }
    }

    private static void LinearEquation(double a, double b)
    {
        if (a == 0)
        {
            Console.WriteLine("\"a\" should not be 0!");
        }
        else
        {
            if (b > 0)
            {
                Console.WriteLine("The equation is: {0} * x + {1} = 0", a, b);
            }
            else if (b < 0)
            {
                Console.WriteLine("The equation is: {0} * x {1} = 0", a, b);
            }
            else
            {
                Console.WriteLine("The equation is: {0} * x = 0", a, b);
            }
            double x = (-b) / a;
            Console.WriteLine("x = {0:0.00}", x);
        }
    }

    private static double Average(int size, int[] array)
    {
        //print the array
        for (int i = 0; i < array.Length; i++)
        {
            Console.Write(array[i] + " ");
        }
        Console.WriteLine();

        int sum = 0;
        double result = 0;
        for (int i = 0; i < array.Length; i++)
        {
            sum = sum + array[i];
        }

        return result = (double)sum / (double)size;
    }

    private static void ReverseDigits(string number, int numberInt)
    {
        while (numberInt > 0)
        {
            for (int i = number.Length - 1; i >= 0; i--)
            {
                Console.Write(number[i]);
            }
            Console.WriteLine();
            return;
        }
    }

}

